﻿using DeviceMaintainGIS.Entity;
using DeviceMaintainGIS.Model;
using DeviceMaintainGIS.Model.Document;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DeviceMaintainGIS.BLL.Document
{
    /// <summary>
    /// 资料操作
    /// </summary>
    public class DocumentService
    {
        /// <summary>
        /// 根据条件查询资料列表
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        // POST: api/Document
        public ReturnListVM<DocumentModel> GetDocumentList(DocumentRequest request)
        {
            ReturnListVM<DocumentModel> list = new ReturnListVM<DocumentModel>();
            using (CYOADBEntities Entities = new CYOADBEntities())
            {

                var linq = from a in Entities.Document_Tab
                           join m in Entities.DocumentType_Tab on a.DocumentTypeId equals m.DocumentTypeId
                            into tmpDocumentType
                           let s = tmpDocumentType.FirstOrDefault()
                           select new DocumentModel
                           {
                               DocumentId = a.DocumentId,
                               DocumentName = a.DocumentName,
                               DocumentTypeName = s.DocumentType,
                               IsSecrecy = a.IsSecrecy == true ? "是" : "否",
                               DownloadUrl = a.DownloadUrl,
                               AddTime = a.AddTime

                           };


                //设备名称
                if (!string.IsNullOrEmpty(request.DocumentName))
                {
                    linq = linq.Where(i => i.DocumentName.Contains(request.DocumentName));
                }

                list.TotalCount = linq.Count();
                list.List = linq.OrderBy(i => i.DocumentId).Take(request.PagesSize * request.PageIndex).Skip(request.PagesSize * (request.PageIndex - 1)).ToList();
                list.PageIndex = request.PageIndex;
                list.PagesSize = request.PagesSize;

            }
            return list;
        }
        public ReturnListVM<DocumentModel> GetDocumentExtendList(DocumentExtendRequest request)
        {
            ReturnListVM<DocumentModel> list = new ReturnListVM<DocumentModel>();
            using (CYOADBEntities Entities = new CYOADBEntities())
            {

                var linq = from a in Entities.DocumentRelation_Tab
                           join d in Entities.Document_Tab on a.DocumentId equals d.DocumentId
                            into tmpDocument
                           let DocumentTab = tmpDocument.FirstOrDefault()
                           join m in Entities.DocumentType_Tab on DocumentTab.DocumentTypeId equals m.DocumentTypeId
                            into tmpDocumentType
                           let s = tmpDocumentType.FirstOrDefault()
                           select new DocumentModel
                           {
                               DocumentId = a.DocumentId,
                               DocumentName = DocumentTab.DocumentName,
                               DocumentTypeName = s.DocumentType,
                               IsSecrecy = DocumentTab.IsSecrecy == true ? "是" : "否",
                               DownloadUrl = DocumentTab.DownloadUrl,
                               AddTime = DocumentTab.AddTime,
                               LPDId = a.LPDId,
                               Type = a.Type

                           };


                //设备名称
                if (!string.IsNullOrEmpty(request.LPDId))
                {
                    linq = linq.Where(i => i.LPDId == request.LPDId && i.Type == request.Type);
                }

                list.TotalCount = linq.Count();
                list.List = linq.OrderBy(i => i.DocumentId).Take(request.PagesSize * request.PageIndex).Skip(request.PagesSize * (request.PageIndex - 1)).ToList();
                list.PageIndex = request.PageIndex;
                list.PagesSize = request.PagesSize;

            }
            return list;
        }
    }
}
